package com.sdonkey.score.controller;

import com.mongodb.util.JSON;
import com.sdonkey.score.dto.UserInfoDto;
import com.sdonkey.score.dto.register.RegisterPageDetailedInfo;
import com.sdonkey.score.dto.register.RegisterPageLoginInfo;
import com.sdonkey.score.service.IUserLoginService;
import com.sdonkey.score.service.IUserRegisterService;
import com.sdonkey.score.util.FormValidateUtil;
import com.sdonkey.score.util.JsonUtil;
import com.sdonkey.score.util.ValidateCodeUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.json.Json;
import javax.servlet.http.HttpSession;

/**
 * 用户注册模块
 *
 * @author ZhaoShihao
 * @version 1.0
 */
@Controller
@RequestMapping(value = "/register", method = RequestMethod.POST)
public class UserRegisterController {
    private Logger logger = Logger.getLogger(UserRegisterController.class);
    @Autowired
    private IUserRegisterService registerService;
    @Autowired
    private IUserLoginService loginService;

    /**
     * 发送手机验证码
     *
     * @param mobile 用户手机号
     * @return
     */
    @RequestMapping("/ajaxGetCodeToMobile")
    @ResponseBody
    public String ajaxGetCodeToMobile(String mobile, HttpSession session) {
        Integer id = loginService.hasPhone(mobile);
        if (id == null) {
            int i = ValidateCodeUtil.writeCodeToMobile(mobile, session);
            if (i == 0) {
                logger.info("新用户通过手机号" + mobile + " 注册 发送验证码");
                return "1";
            } else {
                return "0";
            }
        }
        return "-1";
    }

    /**
     * 用户注册-填写手机号
     *
     * @param mobile 用户手机号
     * @param code   验证码
     * @return
     */
    @RequestMapping(value = "/ajaxRegisterForMobile")
    @ResponseBody
    public String ajaxRegisterForMobile(String mobile, String code, HttpSession session) {
        String sysCode = (String) session.getAttribute(mobile);
        if (FormValidateUtil.isCode(sysCode, code, false)) {
            session.setMaxInactiveInterval(60 * 30);
            return "1";
        }
        return "-1";
    }

    /**
     * 用户注册-填写账号信息
     *
     * @param info 账号信息
     * @return
     */
    @RequestMapping("/ajaxRegisterForLogin")
    @ResponseBody
    public String ajaxRegisterForLogin(RegisterPageLoginInfo info, HttpSession session) {
        String code = info.getCode();
        String sysCode = (String) session.getAttribute(info.getMobile());
        //验证校验码信息，判断用户是否为正常注册
        if (!FormValidateUtil.isCode(sysCode, code, true)) {
            return "-1";
        }
        //验证注册手机号是否存在
        Integer id = loginService.hasPhone(info.getMobile());
        if (id == null) {
            registerService.insertUser(info);
            id = info.getId();
            if (id == null) {
                return "0";
            } else {
                session.setAttribute("rid", id);
            }
            logger.info("用户" + id + " 注册 填写基本信息");
        }
        session.removeAttribute(info.getMobile());
        return "1";
    }

    /**
     * 用户注册-填写详细信息
     *
     * @param user 账号信息
     * @return
     */
    @RequestMapping("/ajaxRegisterForDetailedInfo")
    @ResponseBody
    public String ajaxRegisterForDetailedInfo(String user, HttpSession session) {
        RegisterPageDetailedInfo info = JsonUtil.readValue(user, RegisterPageDetailedInfo.class);
        if (info != null) {
            String code = (String) session.getAttribute(info.getEmail());
            if (FormValidateUtil.isCode(code, info.getCode(), true)) {
                session.setMaxInactiveInterval(60 * 30);
                Integer id = (Integer) session.getAttribute("rid");
                Integer emailId = loginService.hasEmail(info.getEmail());
                if (emailId == null || id.equals(emailId)) {
                    info.setId(id);
                    registerService.updateUserDetailedInfo(info);
                    logger.info("用户" + id + " 注册 填写详细信息");
                    return "1";
                }
            }
        }
        return "-1";
    }

    /**
     * 发送邮箱验证码
     *
     * @param email 用户邮箱
     * @return
     */
    @RequestMapping("/ajaxGetCodeToEmail")
    @ResponseBody
    public String ajaxGetCodeToEmail(String email, HttpSession session) {
        Integer id = loginService.hasEmail(email);
        if (id == null) {
            id = (Integer) session.getAttribute("rid");
            UserInfoDto user = loginService.getUser(id);
            int i = ValidateCodeUtil.writeCodeToEmail(email, user.getName(), session);
            if (i == 0) {
                logger.info("用户" + id + " 邮箱" + email + " 获取验证码");
                return "1";
            } else {
                return "0";
            }
        }
        return "-1";
    }

    @RequestMapping("/ajaxValidateCodeForEmail")
    @ResponseBody
    public String ajaxValidateCodeForEmail(String email, String code, HttpSession session) {
        String sysCode = (String) session.getAttribute(email);
        return FormValidateUtil.isCode(sysCode, code, true) ? "1" : "-1";
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @RequestMapping("/ajaxGetUserByRegister")
    @ResponseBody
    public UserInfoDto ajaxGetUserByRegister(HttpSession session) {
        Integer id = (Integer) session.getAttribute("rid");
        String rtk = (String) session.getAttribute("rtk");
        return loginService.getUser(id);
    }

}
